// pages/order/order.js
const app = getApp();
Page({
  data: {
    goodsList: [],
    number: [],
    list: [],
    counter: 0,
    price: 0,
    orderList: [{
      goodsId: 0,
      number: 0
    }],
    consignee: '',
    phone: '',
    dormitory: ''
  },
  onLoad: function(options) {
    console.log(options)
    this.setData({
      goodsList: app.globalData.goodsList,
      counter: options.counter,
      price: options.price
    })
    console.log("确定订单页面：", this.data.goodsList)
    //初始化已选好的货品
    const oldList = this.data.list;
    const oldNumber = this.data.number;
    for (var i = 0; i < this.data.goodsList.length; i++) {
      for (var j = 0; j < this.data.goodsList[i].countList.length; j++) {
        if (this.data.goodsList[i].countList[j] != 0) {
          oldList.push(this.data.goodsList[i].goods[j])
          oldNumber.push(this.data.goodsList[i].countList[j])
        }
      }
    }
    this.setData({
      [`list`]: oldList,
      [`number`]: oldNumber
    })
    console.log("--------", this.data.list)
  },
  //确认下单
  orderSure() {
    console.log("确认下单")
    var consignee = this.data.consignee
    var phone = this.data.phone
    var dormitory = this.data.dormitory
    if (this.data.consignee != '' &&
      this.data.phone != '' && this.data.phone.length == 11 &&
      this.data.dormitory != '') {
      var orderList = []
      for (var i = 0; i < this.data.list.length; i++) {
        orderList.push({
          goodsId: this.data.list[i].id,
          number: this.data.number[i]
        })
      }
      this.setData({
        [`orderList`]: orderList
      })
      wx.showLoading({
        title: '加载中...',
      })
      var token = wx.getStorageSync('token')
      var goodsJsonStr = JSON.stringify(this.data.orderList)
      wx.request({
        url: 'https://api.it120.cc/kehuafu/order/create',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 请求头
        },
        data: {
          remark: '【C19-' + dormitory + '】' + consignee + '-' + phone,
          goodsJsonStr: goodsJsonStr,
          payOnDelivery: 1, //货到付款
          token: token
        },
        success(res) {
          console.log("下单：", res.data.msg)

          if (res.data.msg == 'success') {
            wx.reLaunch({
              url: '/pages/mine/mine'
            })
          } else {
            wx.hideLoading()
            wx.showToast({
              title: '提交失败',
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '收货信息有误~',
      })
    }
  },
  //收货人
  onConsignee(event) {
    // event.detail 为当前输入的值
    console.log("收货人:", event.detail.value.replace(/\s+/g, ''));
    this.setData({
      consignee: event.detail.value.replace(/\s+/g, '')
    })
  },
  //联系电话
  onPhone(event) {
    // event.detail 为当前输入的值
    console.log("收货人:", event.detail.value.replace(/\s+/g, ''));
    this.setData({
      phone: event.detail.value.replace(/\s+/g, '')
    })
  },
  //收获地址
  onDormitory(event) {
    // event.detail 为当前输入的值
    console.log("收货人:", event.detail.value.replace(/\s+/g, ''));
    this.setData({
      dormitory: event.detail.value.replace(/\s+/g, '')
    })
  }
})